package com.zgzg.shop.pay.business.common.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * Swagger 配置
 */
@EnableWebMvc
@Configuration
@Profile({"local", "dev", "DEV", "fat", "FAT", "UAT"})
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
@Slf4j
@ConditionalOnProperty(name = "enabled", prefix = "swagger",havingValue = "true",matchIfMissing = false)
public class SwaggerConfig  {

    @Resource
    private SwaggerProperty swaggerProperty;

    @ConditionalOnMissingBean(Docket.class)
    @Bean
    public Docket createRestApi() {
        log.info("配置================> swagger文档");
        return new Docket(DocumentationType.SWAGGER_2)
                .globalRequestParameters(requestParameterBuilderList())
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerProperty.getBasePackage()))
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title(swaggerProperty.getTitle())
                .description(swaggerProperty.getDescription())
                .version(swaggerProperty.getVersion())
                .build();
    }

    private ArrayList<RequestParameter> requestParameterBuilderList(){
        return Lists.newArrayList(
                new RequestParameterBuilder()
                        .name("X-tmy-username")
                        .description("username")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("nbg@8531.com")
                                .allowEmptyValue(false))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-tmy-realName")
                        .description("realName")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("大洋测试1")
                                .allowEmptyValue(false))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-tmy-tenantId")
                        .description("tenantId")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("102")
                                .allowEmptyValue(true))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-tmy-userId")
                        .description("userId")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("419")
                                .allowEmptyValue(true))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-tmy-groupId")
                        .description("groupId")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("0")
                                .allowEmptyValue(true))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-tmy-userType")
                        .description("userType")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("0")
                                .allowEmptyValue(true))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-tmy-systemCode")
                        .description("systemCode")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("TMY")
                                .allowEmptyValue(true))
                        .build(),
                new RequestParameterBuilder()
                        .name("X-tmy-idsGroupId")
                        .description("idsGroupId")
                        .in(ParameterType.HEADER)
                        // 类内部创建了默认的builder以供属性设置
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("0,1,2")
                                .allowEmptyValue(true))
                        .build(),
                new RequestParameterBuilder()
                        .name("Authorization")
                        .description("token")
                        .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("bearer abc")
                                .allowEmptyValue(true))
                        .in(ParameterType.HEADER)
                        .build());
    }
}